package com.hequ.merchant.banner.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;

import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.volley.VolleyUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.hequ.merchant.banner.R;

import java.io.InputStream;

/**
 * 利用Volley加载图片工具类
 * Created by 柯尊诚 on 2015/10/30.
 * kzc
 */
public class NetworkImageUtil {


    private final RequestQueue requestQueue;
    private Context context;

    private static NetworkImageUtil instance;

    public NetworkImageUtil(Context context) {
        this.context = context;
        requestQueue = Volley.newRequestQueue(context);
        requestQueue.start();
        //自动缓存处理
        Glide.get(context).register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(requestQueue));
    }


    public static NetworkImageUtil getInstance(Context context) {
        if (instance == null) {
            instance = new NetworkImageUtil(context);
        }
        return instance;
    }


    /**
     * 利用Volley异步加载图片
     * <p/>
     * 注意方法参数:
     * getImageListener(ImageView view, int defaultImageResId, int errorImageResId)
     * 第一个参数:显示图片的ImageView
     * 第二个参数:默认显示的图片资源
     * 第三个参数:加载错误时显示的图片资源
     */
    public void loadImageByVolley(String imageUrl, ImageView mImageView) {
//        String imageUrl = "http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";

        final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);
        ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public void putBitmap(String key, Bitmap value) {
                lruCache.put(key, value);
            }

            @Override
            public Bitmap getBitmap(String key) {
                return lruCache.get(key);
            }
        };
        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
        ImageListener listener = ImageLoader.getImageListener(mImageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        imageLoader.get(imageUrl, listener);
    }

    /**
     * 利用NetworkImageView显示网络图片
     */
    public void showImageByNetworkImageView(String imageUrl, NetworkImageView mNetworkImageView) {
//        String imageUrl = "http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";
        final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);
        ImageCache imageCache = new ImageCache() {
            @Override
            public void putBitmap(String key, Bitmap value) {
                lruCache.put(key, value);
            }

            @Override
            public Bitmap getBitmap(String key) {
                return lruCache.get(key);
            }
        };
        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
        mNetworkImageView.setTag("url");
        mNetworkImageView.setImageUrl(imageUrl, imageLoader);
    }
}
